package com.biz.primus.management.order.controller;

import com.biz.primus.base.vo.PageResult;
import com.biz.primus.management.order.feign.OmsFeignClient;
import com.biz.primus.model.oms.enums.OrderDeliveryType;
import com.biz.primus.model.oms.enums.OrderPaymentState;
import com.biz.primus.model.oms.enums.OrderState;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import com.biz.primus.model.oms.vo.OrderInterceptQueryVo;
import com.biz.primus.model.oms.vo.OrderInterceptVo;
import com.biz.primus.model.oms.vo.OrderQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @note: 订单拦截管理控制层
 * @author: zzy
 * @careat: 2017-11-22 17:37
 **/
@Controller
@RequestMapping("interception")
public class InterceptionController {

    @Autowired
    private OmsFeignClient omsFeignClient;

    /**
     * 订单拦截管理(首页,跳转)
     * @return
     */
    @GetMapping
    public ModelAndView interceptionIndex () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("interception/list");
        return modelAndView;
    }

    /**
     * 新增拦截(新增拦截管理,跳转)
     * @return
     */
    @GetMapping("interceptionAdd")
    public ModelAndView interceptionAdd () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("interception/add");
        return modelAndView;
    }

    /**
     * 订单拦截管理(处理页面获取数据,刷新)
     * @return 前端使用datatable 必须使用aadata为key返回
     * @Exc     分页参数错误
     */
    @GetMapping("interceptionList")
    @ResponseBody
    public Map<String, Object> interceptionList (OrderDeliveryType state, String orderCode, int start, int length) {
        // 分页数据判断(datatable 分页数是0开始的)
        if (start < 0 && length < 1) {
            try {
                throw new Exception("分页参数错误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        OrderInterceptQueryVo orderQueryVo = new OrderInterceptQueryVo();

        List<OrderState> orderQueryVos = new LinkedList<>();

        // 查询=>已分单
        orderQueryVos.add(OrderState.allocated);

        // 添加分页数据
        orderQueryVo.setPage(start+1);
        orderQueryVo.setSize(length);

        Map<String, Object> result = new HashMap<>();
//        PageResult<OrderInterceptVo> omsOrderVoSpringPageVO = omsFeignClient.findAllOrderIntercept(orderQueryVo);
//        //数据总条数
//        result.put("iTotalRecords", omsOrderVoSpringPageVO.getTotalPage());
//        //显示的条数
//        result.put("iTotalDisplayRecords", omsOrderVoSpringPageVO.getCurrentPage());
//        result.put("aaData", omsOrderVoSpringPageVO.getContent());

        List<OmsOrderVo> omsOrderVoSpringPageVO  = getAll();
        // 返回值
        result.put("iTotalRecords", omsOrderVoSpringPageVO.size());//数据总条数
        result.put("iTotalDisplayRecords", omsOrderVoSpringPageVO.size());//显示的条数
        result.put("aaData", omsOrderVoSpringPageVO);
        return result;
    }

    private List<OmsOrderVo> getAll() {
        List<OmsOrderVo>  result = new LinkedList<>();
        for (int j = 0; j < 6; j++) {
            OmsOrderVo orderItemVO = new OmsOrderVo();
            orderItemVO.setCode("订单编码" + j);
            orderItemVO.setOrderSource("订单来源" + j);
            orderItemVO.setSelfRemark("商家备注" + j);
            orderItemVO.setDeliveryType(OrderDeliveryType.self_now);
            orderItemVO.setPaymentState(OrderPaymentState.cod);
            orderItemVO.setAmount(j);
            orderItemVO.setDeliveryType(OrderDeliveryType.self_now);
            result.add(orderItemVO);
        }
        return result;
    }
}
